package blog.farb.top.springbootforredis.repo;

import blog.farb.top.springbootforredis.entity.Stock;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class StockRedisRepo {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public Stock getStockById(String key) {
        String jsonStr = redisTemplate.opsForValue().get(key);
        return JSON.parseObject(jsonStr, Stock.class);
    }

    public void addStock(String key, Stock stock, int expireSeconds) {
        redisTemplate.opsForValue().set(key, JSON.toJSONString(stock), expireSeconds);
    }
}
